<?php 
class Account extends BZ_Controller
{
   public function _init() {
        
        $this->load->helper(array('city','common'));
        $this->load->model('advert_model','advert');
        $this->load->model('line_base_model', 'line_base');
        $this->load->model('line_travel_type_model', 'line_travel_type');
        $this->load->model('line_product_type_model', 'line_product_type');
        $this->load->model('line_destination_model', 'line_destination');
        $this->load->model('line_related_destination_model', 'line_related_destination');
        $this->load->model('cms_block_model', 'cms_block');
   }
    
   public function grid() 
   {
        $type = $this->input->get('type') ? $this->input->get('type') : 1;
        $currentCity = get_current_city();
        $data = array(
            'lineTravelType'   => $this->line_travel_type->find(),
            'lineWapAdvertise' => $this->advert->findByParams(['status'=> 1, 'opentype' => 'link', 'position' => 10], '*', null, 'sort ASC'),
            'city'             => $currentCity,
            'type'             => $type,
            'cmsBlocks'        => $this->cms_block->findByBlockIds(array('wap_dujia_home_hotcity', 'wap_dujia_home_selectcity')),
        );
        //推荐人
        $username = $this->input->get('username') ? trim($this->input->get('username')) : 'bz_teck';
        if ($username != 'bz_teck') {
            set_cookie('true_user', $username, 43250);
        }
        $this->load->view('line/account/grid', $data);
   }
   
   public function view()
   {
        $pageNum  =  15;
        $getData  =  $this->input->get();
        $pg       =  !empty($getData['page']) ? $getData['page'] : '1';
        $num      =  ($pg - 1)*$pageNum;
        $total    =  $this->line_base->findCatalogTotal($getData);
        $data['count']    =  ceil($total/$pageNum);
        $data['lineRseult'] = $this->line_base->findCatalogPageList($pageNum,$num,$getData);
        $data['use_base_url'] = 'on';
        $data['back_url'] = 'javascript:history.back()';
        $data['line_product_type'] = $this->line_product_type->find();

        $data['line_travel_type']  = lineTravelType($type=true);
        $result = $this->line_base->findCatalogPageList1($this->input->get()); 
        $catalog_list = $this->getAllNeedHtml($result);
        $data['travel_day_search'] = $catalog_list['travelDays']; //导航搜索确定有效天数
        $data['product_type_search'] = $catalog_list['lineProductType']; //导航搜索确定有效产品类型
   
        $data['travel_type'] = isset($getData['line_travel_type']) ? $getData['line_travel_type'] :'';
        $data['address'] = isset($getData['address']) ? $getData['address']:'';
        $data['day'] = isset($getData['travel_day']) ? $getData['travel_day']:'';
        $data['type'] = isset($getData['line_product_type']) ? $getData['line_product_type'] : '';
        $data['price_range'] = isset($getData['price_range']) ? $getData['price_range'] : '';
        
        $data['name'] = $this->input->get('address') ? $this->input->get('address') : $this->input->get('start_address');
        $data['page'] = $pg;
        
        if( $data['lineRseult']->num_rows() > 0 ) {
       	   if( $pg == '1') {
       	   	   $this->load->view('line/account/view',$data);
       	   }else{
       	   	   echo json_encode(array('status'=>true,'content'=>$this->load->view('line/account/viewlist',$data,true)));exit;
       	   }
        }else{
       	   if( $pg == '1') {
       	       $this->load->view('line/searchresult/noresult',$data);
       	   }else{
       	   	   echo json_encode(array('status'=>false,'content'=>'暂无内容'));exit;
       	   }
        }
   }

   /**
    * @param  unknown $line_id  线路id
    * @return multitype: 获得目的地城市
    */
   public function getNeedDestination($line_id) 
   {
       $destination = array();
       if($line_id->num_rows() > 0) {
   	       foreach ( $line_id->result() as $item ) {
   	           $line_ids[] = $item->id;
   	       }
   	       $result = $this->line_related_destination->getLineIdsDestination($line_ids);
   	       if( $result->num_rows() > 0 ) {
   	           foreach ( $result->result() as $val ) {
   	               $destination[] = $val->address;
   	           }
   	           $destination = array_count_values($destination);
   	       }
   	   }
   	   return  $destination;
   }
   
   /**
    * @param unknown $result-- 景区结果
    */
   public function getAllNeedHtml($result)
   {
      $allNeedHtml = array(
               'travelDays'       => array(),
               'lineProductType'  => array(),
               'lineStartAddress' => array(),
      );
      if($result->num_rows() > 0) {
          foreach($result->result() as $item){
              $allNeedHtml['travelDays'][] = $item->travel_days;
              $allNeedHtml['lineProductType'][] = $item->line_product_type;
              $allNeedHtml['lineStartAddress'][] = $item->start_address;
          }
          $allNeedHtml['travelDays'] =  array_count_values($allNeedHtml['travelDays']);
          $allNeedHtml['lineProductType'] = array_count_values($allNeedHtml['lineProductType']);
          $allNeedHtml['lineStartAddress'] = array_count_values($allNeedHtml['lineStartAddress']);
      }
      return $allNeedHtml;
   }
   
   public function ajaxGetLineList()
   {
       $pageNum  =  15;
       $getData  =  $this->input->get();
       $pg       =  !empty($getData['page']) ? $getData['page'] : '1';
       $num      =  ($pg - 1)*$pageNum;
       $total    =  $this->line_base->findCatalogTotal($getData);
       $count    =  ceil($total/$pageNum);
       $data['line_product_type'] = $this->line_product_type->find();
       $data['line_travel_type']  = lineTravelType($type=true);
       $data['lineRseult'] = $this->line_base->findCatalogPageList($pageNum,$num,$getData);
       if( $data['lineRseult']->num_rows() > 0 ) {
           echo json_encode(array('status'=>true,'count'=>$count,'content'=>$this->load->view('line/account/viewlist',$data,true)));exit;
       }else{
       	   echo json_encode(array('status'=>false,'count'=>$count,'nocontent'=>$this->load->view('line/account/viewnoresult',$data,true)));exit;
       }
   }
   
   public function ajaxGetDhSearch()
   {
       $type = $this->input->get('type') ? $this->input->get('type') : '1';
       $city = $this->input->get('city') ? str_replace('市','',$this->input->get('city')) : get_current_city();
       $line_id = $this->line_base->findDhLineMark($city,$type);
       $data['destination'] = $this->getNeedDestination($line_id);
       $data['type'] = $type;
       $data['city'] = $city;
       echo json_encode(array('status'=>true,'content'=>$this->load->view('line/account/ajaxGetDhSearch',$data,true)));exit;
   }
}